import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询材料供应商资料
 */
export async function pageMesMaterialSuppliers(params) {
  const res = await request.get('/mes/mesMaterialSupplier/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部材料供应商资料
 */
export async function listMesMaterialSuppliers(params) {
  const res = await request.get('/mes/mesMaterialSupplier/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询材料供应商资料
 */
export async function getMesMaterialSupplier(id) {
  const res = await request.get('/mes/mesMaterialSupplier/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加材料供应商资料
 */
export async function addMesMaterialSupplier(data) {
  const res = await request.post('/mes/mesMaterialSupplier', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改材料供应商资料
 */
export async function updateMesMaterialSupplier(data) {
  const res = await request.put('/mes/mesMaterialSupplier', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除材料供应商资料
 */
export async function removeMesMaterialSupplier(id) {
  const res = await request.delete('/mes/mesMaterialSupplier/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除材料供应商资料
 */
export async function removeMesMaterialSuppliers(ids) {
  const res = await request.delete('/mes/mesMaterialSupplier/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出材料供应商资料
 */
export async function exportMesMaterialSuppliers(params) {
  const res = await request({
    url: '/mes/mesMaterialSupplier/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesMaterialSupplier_${Date.now()}.xlsx`);
}
